package net.gillessed.icarus.swingui.color;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.swing.JOptionPane;

public class GradientProvider {
	
	public static final String GRADIENT_SOURCE_FILE_NAME = "gradients.dat";
	
	private final Map<String, Gradient> gradientMap;
	private String defaultGradient;
	
	public GradientProvider(String filename) {
		gradientMap = new HashMap<String, Gradient>();
		defaultGradient = "basic-1";
		//TODO: read in gradient data.
		

		try {
			File src = new File(GradientProvider.GRADIENT_SOURCE_FILE_NAME);
			
			if(!src.exists()) {
				JOptionPane.showMessageDialog(null, "You do not have a gradient source file. Creating one now with some default gradients.", "Warning", JOptionPane.INFORMATION_MESSAGE, null);
				createDefaultFile();
			}
			
			FileInputStream fin = new FileInputStream(src);
			ObjectInputStream din = new ObjectInputStream(fin);
			int count = din.readInt();
			for(int i = 0; i < count; i++) {
				Gradient g = (Gradient)din.readObject();
				gradientMap.put(g.getName(), g);
			}
			din.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	private void createDefaultFile() throws IOException, ClassNotFoundException {
		SortedMap<Integer, Color> map = new TreeMap<Integer, Color>();
		map.put(200,new Color(0,200,200));
		map.put(300,new Color(204,0,150));
		gradientMap.put("basic-1", new Gradient(new Color(0,12,203),
				new Color(203,12,3),
				map,
				Gradient.DEFAULT_SIZE,
				"basic-1"));

		map = new TreeMap<Integer, Color>();
		map.put(300, new Color(100,200,200));
		gradientMap.put("basic-2", new Gradient(new Color(205,12,203),
				new Color(100,200,30),
				map,
				Gradient.DEFAULT_SIZE,
				"basic-2"));

		map = new TreeMap<Integer, Color>();
		map.put(300, new Color(200,200,50));
		map.put(350, new Color(200,100,0));
		gradientMap.put("basic-3", new Gradient(new Color(205,255,24),
				new Color(200,0,230),
				map,
				Gradient.DEFAULT_SIZE,
				"basic-3"));

		map = new TreeMap<Integer, Color>();
		map.put(200, new Color(45,255,200));
		map.put(350, new Color(255,45,0));
		gradientMap.put("basic-4", new Gradient(new Color(0,45,255),
				map,
				Gradient.DEFAULT_SIZE,
				"basic-4"));

		map = new TreeMap<Integer, Color>();
		map.put(200, new Color(0,255,0));
		map.put(350, new Color(255,0,0));
		gradientMap.put("basic-5",new Gradient(new Color(0,0,255),
				map,
				Gradient.DEFAULT_SIZE,
				"basic-5"));
		

		File dest = new File(GradientProvider.GRADIENT_SOURCE_FILE_NAME);
		FileOutputStream fout = new FileOutputStream(dest);
		ObjectOutputStream dos = new ObjectOutputStream(fout);
		dos.writeInt(getGradientMap().size());
		for(Gradient g : getGradientMap().values()) {
			dos.writeObject(g);
		}
		dos.close();
		gradientMap.clear();
	}

	public Map<String, Gradient> getGradientMap() {
		return gradientMap;
	}

	public void setDefaultGradient(String defaultGradient) {
		this.defaultGradient = defaultGradient;
	}

	public String getDefaultGradient() {
		return defaultGradient;
	}
	
	public Gradient getDefault() {
		return gradientMap.get(defaultGradient);
	}

	public Gradient getGradient(String gradientName) {
		return gradientMap.get(gradientName);
	}
	
	public void addNewGradient(Gradient g) {
		gradientMap.put(g.getName(), g);
	}
	
	public void removeGradient(Gradient g) {
		gradientMap.remove(g.getName());
	}
}
